package cn.hengzq.orange.common.response;

import cn.hengzq.orange.common.constant.GlobalErrorCodeConstant;
import cn.hengzq.orange.common.exception.ErrorCode;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.v3.oas.annotations.media.Schema;

/**
 * @author hengzq
 */
@Schema(description = "API统一响应格式")
public record ApiResponse<T>(

        @Schema(description = "业务状态码，200 表示成功", example = "200")
        String code,

        @Schema(description = "响应消息", example = "OK")
        String msg,

        @Schema(description = "响应数据")
        T data
) {

    @JsonIgnore
    public boolean isSuccess() {
        return GlobalErrorCodeConstant.SUCCESS.getCode().equals(code);
    }

    @JsonIgnore
    public boolean isFailed() {
        return !isSuccess();
    }

    public static <T> ApiResponse<T> ok(T data) {
        return new ApiResponse<>(GlobalErrorCodeConstant.SUCCESS.getCode(), "OK", data);
    }

    public static ApiResponse<Void> ok() {
        return ok(null);
    }

    public static <T> ApiResponse<T> fail(ErrorCode errorCode) {
        return new ApiResponse<>(errorCode.getCode(), errorCode.getMsg(), null);
    }


}